package graphics.effects;
import org.lwjgl.opengl.GL11;

import main.Config;
import graphics.system.Color;
import graphics.system.GLTools;
import graphics.system.Texture;
import graphics.system.TextureContainer;
import tools.Floatmath;
import tools.Point;
public class GEExplosion extends GEffect {
	private Texture tex;
	private float radius;
	private Texture wtex;
	public GEExplosion(Point pos, float radius) {
		super(pos, radius, 500, true);
		tex = TextureContainer.getTexture("lightmap");
		wtex = TextureContainer.getTexture("explosionmap");
		this.radius = radius * 5;
		node.pos = new Point(pos.x, pos.y);
		node.setBoundingRad(radius);
		color = new Color(1, 1, 0, 1);
		int a = (int) radius / 10;
		for (int i = 0; i < 20 * a; i++) {
			Point r = new Point(Floatmath.random(), Floatmath.random()).sub(new Point(0.5f, 0.5f)).mul(Floatmath.random() * radius);
			PartSys.sparks.emitParticle(pos.add(r), r.mul(radius * 0.003f), 4);
		}
		for (int i = 0; i < 5 * a; i++) {
			Point r = new Point(Floatmath.random(), Floatmath.random()).sub(new Point(0.5f, 0.5f)).mul(Floatmath.random() * radius);
			PartSys.fire.emitParticle(pos.add(r), r.mul(radius * 0.01f), 2);
		}
		for (int i = 0; i < a / 2; i++) {
			Point r = new Point(Floatmath.random(), Floatmath.random()).sub(new Point(0.5f, 0.5f)).mul(Floatmath.random() * radius);
			PartSys.smoke.emitParticle(pos.add(r.mul(0.2f)), r.mul(radius * 0.0006f), 1);
		}
	}
	public void draw() {
		// GLTools.drawCircle(node.pos, radius * (1-getProgress()), 0, Color.blue);
	}
	public float getRadius() {
		return radius * (1 - getProgress());
	}
	public void drawGlow() {
		float p = getProgress();
		float r = radius * (1 - p);
		color.r = 1 - p;
		color.g = Math.max(0, 1 - 2 * p);
		node.boundingrad = r;
		GL11.glPushMatrix();
		node.setUpGl();
		GLTools.drawTexRect(Point.ZERO, r * 2, r * 2, 0, tex.getTexID(), color.withAlpha((1 - p)));
		GL11.glPopMatrix();
	}
	public void drawWarp() {

		if (Config.SHADER_QUALITY != 1 && getProgress() > 0.9f)
			return;
		GLTools.drawTexRect(node.pos.x, node.pos.y, radius * 0.5f, radius * 0.5f, 0,	wtex.getTexID(), new Color(1, 1, 1,(1 - getProgress())));
	}
}
